package com.rcnss.ui.form;

import com.rcnss.data.*;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormFieldFactory;
import com.vaadin.ui.Window.Notification;

public class EditForm extends Form{

    private static final long serialVersionUID = 1L;
    public static final String COMMON_FIELD_WIDTH = "12em";

    public Button saveChanges = new Button("Appliquer");
    public Button cancelChanges = new Button("Annuler");

    public EditForm(final ITable in, FormFieldFactory fieldFactory){
        initLayout();
        BeanItem beanObject = new BeanItem(in);
        this.setFormFieldFactory(fieldFactory);
        this.setItemDataSource(beanObject);
        this.getFooter().addComponent(saveChanges);
        this.getFooter().addComponent(cancelChanges);
        saveChanges.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                try {
                    validate();
                    in.SaveToDB();
                    getWindow().showNotification("Reclamation Sauvegarde",Notification.TYPE_HUMANIZED_MESSAGE);

                } catch (Exception excep) {
                    getWindow().showNotification("Erreur de sauvegarde",
                            "Une erreur est survenue pendant le sauvegarde de votre Reclamtion",
                            Notification.TYPE_ERROR_MESSAGE);
                }
            }
        });

        cancelChanges.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                discard();
            }
        });

        if(Reclamation.class.isInstance(in))
        {
            setVisibleItemProperties(new String[] {
                "REC_ID", "UTL_ID", "etatReclamation", "CAT_ID", "REC_TITRE",
                "REC_MSG", "REC_AFF_A"
            });
        }
        else if(User.class.isInstance(in)) {
            setVisibleItemProperties(new String[] {
                "ID", "matricule", "typeUser", "NOM", "prenom", "login",
                "POST", "codeStructure", "mail", "telBureau", "telMobile"
            });
            this.setSizeUndefined();
        }else if(Intervention.class.isInstance(in)) {
            setVisibleItemProperties(new String[] {
                "INT_ID", "INT_DATE", "REC_ID", "ETAT", "INT_COMMENT"
            });
        }
    }
	
    public void initLayout()
    {
        this.setSizeFull();
        this.setImmediate(true);
    }

}
